/**
 * AppRequestAspect.java
 * Created at 2018-1-4
 * Created by zhanghailin
 * Copyright (C) 2018 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.app.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.broadtext.rms.system.model.VSysUsers;
import com.broadtext.rms.system.service.ILoginService;

/**
 * <p>ClassName: AppRequestAspect</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2018-1-4</p>
 */
@Aspect  
@Component
public class AppRequestAspect {
    
    /**
     * 登录服务
     */
    @Autowired
    @Qualifier("loginService")
    private ILoginService loginService;
    
    
    @Pointcut("@annotation(com.broadtext.common.annotation.AppControllerAspect)") 
    public void appControllerAspect(){
        
    }
    
    /** 
     * 前置通知 用于拦截Controller层记录用户的操作 
     * 
     * @param joinPoint 切点 
     */ 
    @Around("appControllerAspect()")  
    public Object doAround(ProceedingJoinPoint joinPoint)  {  
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
        Object result = new Object();
        //读取session中的用户  
        try {  
            String loginName = request.getParameter("loginName");
            VSysUsers vUser = this.loginService.findVuser(loginName);
            if(vUser == null){
                return null;
            }else {
                result = joinPoint.proceed();
            }
        } catch (Throwable e) {  
            e.printStackTrace();
        } 
        return result;
    } 

}
